package cn.edu.zut.cs.Controller;

import cn.edu.zut.cs.Business.FirendsService;
import cn.edu.zut.cs.Business.UserService;
import cn.edu.zut.cs.Model.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
public class UserpageHelperController {
    @Autowired
    FirendsService firendsService;
    @Autowired
    UserService userService;

    @RequestMapping("/emps")
    public String getEmps(HttpSession session, @RequestParam(value="pn",defaultValue="1") Integer pn, Model model) {
        // String left_name="张三";

        User us= (User) session.getAttribute("userInfo");
        String left_name=us.getUsername();

        //1.使用PageHelper分页插件,在查询之前只需传入页码及每页显示的数据记录参数即可！
        PageHelper.startPage(pn, 8);
        //2.startPage后紧跟的就是一个分页查询 按照当前登录者查询出他的全部好友
        List<User> user=firendsService.findByleftnamelists(left_name);
        //System.out.println(user);
        //3.使用PageInfo封装查询结果，只需将PageInfo交给页面即可
        //4.PageInfo封装分页的详细信息
        //5. PageInfo构造器的5表示连续显示的页码数字
        PageInfo<User> page=new PageInfo<User>(user,5);
        model.addAttribute("pageInfo", page);
        return "friendspage";

    }

    @RequestMapping("deletefirend")
    @ResponseBody
    public Object delete(String left_name,String right_name){

        System.out.println(left_name + right_name);

        // Firends firends = firendsService.findByleftname(left_name,right_name);
//       if (firends==null){
//           System.out.println(1+"......"+"查询失败");
//       }
        HashMap<String, String> hash = new HashMap<String, String>();

        // if (firends != null){
        hash.put("stateCode", "1");
        System.out.println(2+"......"+"查询成功");
        firendsService.deletefriend(left_name,right_name);
        firendsService.deletefriend(right_name,left_name);
        // }else{
        // hash.put("stateCode", "0");
        //}

        return hash;
    }

    @RequestMapping("findfriends")
    @ResponseBody
    public User find(String username, Model model){
        //查找要添加的好友
        User user=userService.findbyusernameandpwd(username);
        if (user==null){
            model.addAttribute("findresult","没有找到");
            return user;
        }
        model.addAttribute("findresult","找到了一些人人");
        return user;
    }
}
